/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package com.isisufes.joinus.appserver;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public abstract class Servlet0 extends HttpServlet
{
	//versão 1, atualizar sempre que mexer no servlet
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest req = null;

    protected abstract void initStmt();

    protected abstract String handle() throws Exception;

    public PreparedStatement ps_new, ps_check;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        req = request;
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        try {
            //if (ps_new == null) {
        	//	out.println("error*2"); // mexi aqui
    	    //}
            String ret = handle();
            out.println("ok");
            out.println(ret);
        } catch (Exception e) {
            out.println("error_at_handle");
            out.println(e.getMessage());
            out.println(e.toString());
            //out.println("teste");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
    	doGet(request, response);
    }

    protected String param(String name) throws Exception
    {
        String ret = req.getParameter(name);
        if (ret == null) throw new Exception("Missing param: " + name);
        return ret;
    }

    public void init(ServletConfig conf) throws ServletException
    {
        super.init(conf);
        DBConn.init();
        Auth.init();
        initStmt();
    }

    public void destroy()
    {
        DBConn.destroy();
        super.destroy();
    }
}
